package se.webbzon.oschi01.collision3d;

import java.util.Collection;

import javax.media.j3d.Transform3D;

public class CollisionGroup3D extends CollisionNode3D {
	
	// An array of all the child nodes 
	private final CollisionNode3D[] children;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new group which covers a collection of collision models. **/
	public CollisionGroup3D(Collection<CollisionNode3D> nodes) {
		super(nodes);
		children = nodes.toArray(new CollisionNode3D[0]);
	}

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public boolean isModel() {
		return false;
	}

	@Override public int children() {
		return children.length;
	}

	@Override public CollisionNode3D getChild(int n) {
		return children[n];
	}
	
	@Override public void setTransform(Transform3D transform) {
		super.setTransform(transform);
		for (int i = 0; i < children.length ; i++)
			children[i].setTransform(transform);
	}
	
	@Override public boolean intersects(CollisionNode3D node) {
		if (super.intersects(node)) {
			if (node.isModel()) {
				for (int i = 0; i < children.length; i++)
					if (children[i].intersects(node))
						return true;
			} else {
				for (int i = 0; i < node.children(); i++)
					if (intersects(node.getChild(i)))
						return true;
			}
		}
		return false;
	}

}
